<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/8/11
 * Time: 16:44
 */

namespace App\Http\Controllers\Web;


use App\Models\Role;
use App\Service\RoleService;

class RoleController extends BaseController
{
    public function index()
    {
        $page = $this->request->post('page',1);
        $limit  = $this->request->post('limit',10);
        $params = $this->request->post();

        return RoleService::getList($params,'id','ASC',$page,$limit);
    }

    public function getRoleById()
    {

        $roleId = $this->request->post('id');
        if(!$roleId) return Response()->json(['code'=>201,'msg'=>'参数缺失']);
        return RoleService::getRoleById($roleId);
    }

    public function add()
    {
        $params = $this->request->post();
        if(!isset($params['name']) || empty(trim($params['name']))){
            return Response()->json(['code'=>201,'msg'=>'角色名不能为空']);
        }
        if(!isset($params['slug']) || empty(trim($params['slug']))){
            return Response()->json(['code'=>201,'msg'=>'角色标识不能为空']);
        }
        return RoleService::add($params);
    }

    public function edit()
    {
        $params = $this->request->post();
        if(!isset($params['id']) || empty(intval($params['id']))){
            return Response()->json(['code'=>201,'msg'=>'参数缺失']);
        }
        if(!isset($params['name']) || empty(trim($params['name']))){
            return Response()->json(['code'=>201,'msg'=>'角色名不能为空']);
        }

        if(!isset($params['slug']) || empty(trim($params['slug']))){
            return Response()->json(['code'=>201,'msg'=>'角色标识不能为空']);
        }

        return RoleService::edit($params);
    }

    public function destroy()
    {
        $ids = $this->request->post('ids');
        $ids = is_array($ids) ? $ids : [$ids];
        if(!$ids){
            return Response()->json(['code'=>201,'msg'=>'请选择要删除的角色']);
        }
        if(in_array(1,$ids)){
            return Response()->json(['code'=>201,'msg'=>'角色为超级管理员不支持删除']);
        }
        return RoleService::destroy($ids);

    }

}
